package com.sc.web.member;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.dal.dto.member.AgentDto;
import com.sc.dal.model.ParamMap;
import com.sc.dal.model.member.Agent;
import com.sc.dal.model.system.UserFormMap;
import com.sc.service.member.AgentService;
import com.sc.service.member.MemberGradeService;
import com.sc.service.member.MemberService;
import com.sc.util.adapter.ConfigBeanAdapter;
import com.sc.util.data.Constants;
import com.sc.util.data.DataUtil;
import com.sc.util.exception.ExceptionEnum;
import com.sc.util.exception.ScBizException;
import com.sc.web.BaseController;
import com.sc.web.plugin.PageView;
@RequestMapping("/agent")
@Controller
public class AgentController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private MemberService memberService;
	@Autowired
	private MemberGradeService memberGradeService;
	@Autowired
	private AgentService agentService;
	
	/**
	 * 
	 * <h2>跳转到会员列表页面</h2> 
	 * 
	 * @param request
	 * @return
	 * @return String 
	 * @throws
	 */
	@RequestMapping(value="/list")
	public String list(HttpServletRequest request,Model model){
		String reviewStatus=getPara("reviewStatus");
		model.addAttribute("discount", memberGradeService.selectList());
		model.addAttribute("res", findByRes());
		request.getSession().setAttribute("reviewStatus", reviewStatus);
		return "member/agent/list";
	}
	@RequestMapping(value="/selectList")
	@ResponseBody
	public PageView selectList(HttpServletRequest request,String pageNow, String pageSize, String column, String sort){
		Map<String,Object> resultMap = new LinkedHashMap<String,Object>();
		
		try{
			//查询代理商列表
			String reviewStatus=(String) request.getSession().getAttribute("reviewStatus");
			String mPhone=request.getParameter("mPhone");
			String agentGrade=request.getParameter("agentGrade");
			ParamMap paramMap = getFormMap(ParamMap.class);
			paramMap.put("reviewStatus", reviewStatus);
			paramMap.put("mPhone", mPhone);
			paramMap.put("agentGrade", agentGrade);			
			paramMap = toFormMap(paramMap, pageNow, pageSize,paramMap.getStr("orderby"));
			paramMap.put("column", column);
			paramMap.put("sort", sort);
			List<AgentDto> agentDtoList = this.agentService.selectList(paramMap);
			pageView.setRecords(agentDtoList);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			logger.info("查询商品数据成功!");
		}catch(Exception e){
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			logger.error("查询商品数据异常:"+e);
			throw new ScBizException("查询商品数据异常！", e);
		}
		return pageView;
	}
	@RequestMapping(value="/addOrEdit")
	public  String addaddOrEdit(String id,Model model){
		try {
			Agent agent=agentService.selectByPrimaryKey(id);
			if(agent==null){
				return "/member/agent/edit";
			}
			agent.setBusinessLicence(agent.getBusinessLicence());
			model.addAttribute("discount", memberGradeService.selectList());
			model.addAttribute("agent", agent);
			return "/member/agent/edit";
		} catch (Exception e) {
			logger.error("查询商品数据异常"+e);
			throw new ScBizException("查询商品数据异常！", e);
		}
		
	}
	
	@RequestMapping(value="/reviewAgent")
	public  String reviewAgent(String id,Model model){
		try {
			Agent agent=agentService.selectByPrimaryKey(id);
			if(agent!=null){
				agent.setBusinessLicence(ConfigBeanAdapter.fileAccessAddress+agent.getBusinessLicence());
			}
			model.addAttribute("discount", memberGradeService.selectList());
			model.addAttribute("agent", agent);
			return "/member/agent/reviewAgent";
		} catch (Exception e) {
			logger.error("查询商品数据异常"+e);
			throw new ScBizException("查询商品数据异常！", e);
		}
		
	}
	@RequestMapping(value="/saveAgent")
	@ResponseBody
	public String saveAgent(HttpServletRequest request,Agent agent){
		try {
			UserFormMap userFormMap=(UserFormMap) SecurityUtils.getSubject().getSession().getAttribute("userSession");
			if("1".equals(agent.getReviewStatus())){
				//把原来较低的代理商等级设为过期
				agent.setModifier(userFormMap.getStr("accountName"));
				agent.setModifyTime(new Date());
				agentService.updateAgentBymId(agent);
			}
			agent.setModifier(userFormMap.getStr("accountName"));
			agent.setModifyTime(new Date());
			agentService.updateAgent(agent);
			return "\"success\"";
		} catch (Exception e) {
			logger.error("查询商品数据异常"+e);
			throw new ScBizException("查询商品数据异常！", e);
		}
	}
	@ResponseBody
	@RequestMapping("deleteEntity")
	public String deleteEntity(){
		try {
			String ids = getPara("ids");
			String[] idArr=ids.split(",");
			for (String string : idArr) {
				Agent agent=new Agent();
				agent.setmId(string);
				agent.setIsDelete(Constants.IS_DELETEB);
				agent.setModifyTime(new Date());
				agentService.updateByPrimaryKeySelective(agent);
			}
			return "\"success\"";
		} catch (Exception e) {
			logger.error("查询商品数据异常"+e);
			throw new ScBizException("查询商品数据异常！", e);
		}
	}
}
